home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / sensible.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-07  |  5.6 KB  |  212 lines

  1. /*
  2.  
  3.         Copyright (C) 1995
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.   You should have received a copy of the GNU General Public License
  20.   along with this program; if not, write to the Free Software
  21.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  22.  
  23. */
  24.  
  25. /*****************************************************************************/
  26. /*                                                                           */
  27. /* File: sensible.c                                                          */
  28. /*                                                                           */
  29. /*****************************************************************************/
  30.  
  31. #include "cf.defs.h"
  32. #include "cf.extern.h"
  33.  
  34. /*********************************************************************/
  35. /* Files to be ignored when parsing directories                      */
  36. /*********************************************************************/
  37.  
  38. char *VSKIPFILES[] =
  39.    {
  40.    ".",
  41.    "..",
  42.    "lost+found",
  43.    ".cfengine.rm",
  44.    NULL
  45.    };
  46.  
  47. /*********************************************************************/
  48.  
  49. SensibleFile(nodename,path,ip)
  50.  
  51. char *nodename, *path;
  52. struct Image *ip;
  53.  
  54. { int i, suspicious = true;
  55.   struct stat statbuf; 
  56.   unsigned char *sp, newname[bufsize];
  57.  
  58. Debug("SensibleFile(%s,%s)\n",path,nodename);
  59.  
  60. if (strlen(nodename) < 1)
  61.    {
  62.    sprintf(OUTPUT,"Empty (null) filename detected in %s\n",path);
  63.    CfLog(cferror,OUTPUT,"");
  64.    return false;
  65.    }
  66.  
  67. if (IsItemIn(SUSPICIOUSLIST,nodename))
  68.    {
  69.    sprintf(OUTPUT,"Suspicious file %s found in %s\n",nodename,path);
  70.    CfLog(cferror,OUTPUT,"");
  71.    return false;
  72.    }
  73.  
  74. if (strcmp(nodename,"...") == 0 && strcmp(path,"/") == 0)
  75.    {
  76.    Verbose("DFS cell node detected in /...\n");
  77.    return true;
  78.    }
  79.   
  80. for (i = 0; VSKIPFILES[i] != NULL; i++)
  81.    {
  82.    if (strcmp(nodename,VSKIPFILES[i]) == 0)
  83.       {
  84.       Debug("Filename is classified as ignorable\n");
  85.       return false;
  86.       }
  87.    }
  88.  
  89. if ((strcmp("[",nodename) == 0) && (strcmp("/usr/bin",path) == 0))
  90.    {
  91.    if (VSYSTEMHARDCLASS == linuxx)
  92.       {
  93.       return true;
  94.       }
  95.    }
  96.  
  97. suspicious = true;
  98.  
  99. for (sp = nodename; *sp != '\0'; sp++)
  100.    {
  101.    if ((*sp > 31) && (*sp < 127))
  102.       {
  103.       suspicious = false;
  104.       break;
  105.       }
  106.    }
  107.  
  108. strcpy(VBUFF,path);
  109. AddSlash(VBUFF);
  110. strcat(VBUFF,nodename); 
  111.  
  112. if (suspicious && NONALPHAFILES)
  113.    {
  114.    sprintf(OUTPUT,"Suspicious filename %s in %s has no alphanumeric content (security)",nodename,path);
  115.    CfLog(cfsilent,OUTPUT,"");
  116.    strcpy(newname,VBUFF);
  117.  
  118.    for (sp = newname+strlen(path); *sp != '\0'; sp++)
  119.       {
  120.       if ((*sp > 126) || (*sp < 32))
  121.      {
  122.      *sp = 50 + (*sp / 50);  /* Create a visible ASCII interpretation */
  123.      }
  124.       }
  125.  
  126.    strcat(newname,".cf-nonalpha");
  127.    
  128.    sprintf(OUTPUT,"Disabling file %s to %s",VBUFF,newname);
  129.    CfLog(cfsilent,OUTPUT,"");
  130.    
  131.    if (rename(VBUFF,newname) == -1)
  132.       {
  133.       CfLog(cfverbose,"Rename failed - foreign filesystem?\n","rename");
  134.       }
  135.    if (chmod(newname,0644) == -1)
  136.       {
  137.       CfLog(cfverbose,"Mode change failed - foreign filesystem?\n","chmod");
  138.       }
  139.    return false;
  140.    }
  141.  
  142. if (strstr(nodename,".") && (EXTENSIONLIST != NULL))
  143.    {
  144.    if (cflstat(VBUFF,&statbuf,ip) == -1)
  145.       {
  146.       sprintf(OUTPUT,"Couldn't examine %s - foreign filesystem?\n",VBUFF);
  147.       CfLog(cfverbose,OUTPUT,"lstat");
  148.       return true;
  149.       }
  150.  
  151.    if (S_ISDIR(statbuf.st_mode))
  152.       {
  153.       for (sp = nodename+strlen(nodename)-1; *sp != '.'; sp--)
  154.      {
  155.      }
  156.  
  157.       if ((char *)sp != nodename) /* Don't get .dir */
  158.      {
  159.      sp++; /* Find file extension, look for known plain files  */
  160.  
  161.      if (IsItemIn(EXTENSIONLIST,sp))
  162.         {
  163.         sprintf(OUTPUT,"Suspicious directory %s in %s looks like plain file with extension .%s",nodename,path,sp);
  164.         CfLog(cfsilent,OUTPUT,"");
  165.         return false;
  166.         }
  167.      }
  168.       }
  169.    }
  170.  
  171. for (sp = nodename; *sp != '\0'; sp++) /* Check for files like ".. ." */
  172.    {
  173.    if ((*sp != '.') && ! isspace(*sp))
  174.       {
  175.       Debug("Filename is not suspicious\n");
  176.       suspicious = false;
  177.       return true;
  178.       }
  179.    }
  180.  
  181. sprintf(OUTPUT,"Suspicous looking file object \"%s\" masquerading as hidden file in %s\n",nodename,path);
  182. CfLog(cfsilent,OUTPUT,"");
  183. Debug("Filename looks suspicious\n"); 
  184.  
  185.  
  186. if (EXTENSIONLIST == NULL)
  187.    {
  188.    if (cflstat(VBUFF,&statbuf,ip) == -1)
  189.       {
  190.       sprintf(OUTPUT,"Couldn't stat %s",VBUFF);
  191.       CfLog(cfverbose,OUTPUT,"lstat");
  192.       return true;
  193.       }
  194.    }
  195.  
  196. if (S_ISLNK(statbuf.st_mode))
  197.    {
  198.    sprintf(OUTPUT,"   %s is a symbolic link\n",nodename);
  199.    CfLog(cfsilent,OUTPUT,"");
  200.    }
  201. else if (S_ISDIR(statbuf.st_mode))
  202.    {
  203.    sprintf(OUTPUT,"   %s is a directory\n",nodename);
  204.    CfLog(cfsilent,OUTPUT,"");
  205.    }
  206.  
  207. sprintf(OUTPUT,"%s has size %d and full mode %o\n",nodename,statbuf.st_size,statbuf.st_mode);
  208. CfLog(cfsilent,OUTPUT,"");
  209.  
  210. return true;
  211. }
  212.